package com.testplatform.aitestplatform.utils;





import com.alibaba.fastjson2.JSONObject;
import com.testplatform.aitestplatform.domain.TestRequset;
import io.restassured.RestAssured;
import io.restassured.config.LogConfig;
import io.restassured.filter.log.RequestLoggingFilter;
import io.restassured.filter.log.ResponseLoggingFilter;
import io.restassured.response.Response;


import java.io.*;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static io.restassured.RestAssured.given;
import static io.restassured.config.JsonConfig.jsonConfig;
import static io.restassured.path.json.config.JsonPathConfig.NumberReturnType.BIG_DECIMAL;

public class BaseTest {
    /**
     * 对get、post、patch、put...做了二次封装
     * @return 接口响应结果
     */
    public static Response request(TestRequset testRequset){

        //接口请求地址
        String url = testRequset.getUrl();
        //请求方法
        String method =testRequset.getMethod();
        //请求头
        Map<String, Object> headers = testRequset.getHeaders();
        //请求参数
        String params = testRequset.getBody();
        //请求头转成Map
//        Map<String,Object> headersMap = JSONObject.parseObject(headers,Map.class);
        Response res = null;
        //对get、post、patch、put做封装
        if("get".equalsIgnoreCase(method)){
            res = given().log().all().headers(headers).when().get(url).then().log().all().extract().response();
        }else if("post".equalsIgnoreCase(method)){
            res= given().log().all().headers(headers).body(params).when().post(url).then().log().all().extract().response();
        }else if("patch".equalsIgnoreCase(method)){
            res= given().log().all().headers(headers).body(params).when().patch(url).then().log().all().extract().response();
        }

        return res;
    }


}
